package com.studentsystem.controller;

import com.studentsystem.common.Result;
import com.studentsystem.model.Course;
import com.studentsystem.service.CourseService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Course)表控制层
 */
@RestController
@RequestMapping("course")
public class CourseController {
    /**
     * 服务对象
     */
    @Resource
    private CourseService courseService;

    @GetMapping("getCourseTable")
    public Result<Course[][]> getCourseTable(@RequestParam("year") Integer year,
                                             @RequestParam("term") Integer term,
                                             @RequestParam("classs") String classs,
                                             @RequestParam("grade") Integer grade,
                                             @RequestParam(required = false, value = "weekday") Integer weekday) {
        List<Course> courses = courseService.queryCourseTable(year, term, classs, grade, weekday);

        Course[][] courseTable = new Course[7][10];
        courses.forEach(course -> {
            if (course == null) {
                return;
            }
            List<String> numbers = course.getNumber();
            for (String number : numbers) {
                courseTable[course.getWeekday() - 1][Integer.parseInt(number) - 1] = course;
            }
        });
        return Result.success(courseTable);
    }

    @GetMapping("getTeachCourses")
    public Result<Object> getTeacherCourses(@RequestParam("year") Integer year,
                                            @RequestParam("term") Integer term,
                                            @RequestParam(required = false, value = "grade") Integer grade,
                                            @RequestParam("teacherName") String teacherName,
                                            @RequestParam("isTable") Integer isTable) {
        if (isTable == null) {
            return Result.fail("请求错误", null);
        }

        List<Course> courses = courseService.queryTeacherCourses(year, term, grade, teacherName);

        if (isTable == 1) {
            Course[][] courseTable = new Course[7][10];
            courses.forEach(course -> {
                if (course == null) {
                    return;
                }
                List<String> numbers = course.getNumber();
                for (String number : numbers) {
                    courseTable[course.getWeekday() - 1][Integer.parseInt(number) - 1] = course;
                }
            });
            return Result.success(courseTable);
        } else if (isTable == 0) {
            return Result.success(courses);
        } else {
            return Result.fail("请求参数错误", null);
        }

    }
}